﻿using ATS.Core.Authorization;
using ATS.Core.Navigation;
using ATS.Core.Settings;
using ATS.GlobalOptions;
using ATS.Services;
using Prism.Ioc;

namespace ATS.Plugins.Template;

public class TemplateModule : IModule
{
    private const string ENTRY_VIEW_NAME = "ATS.Plugins.Template.Views.Main";
    private const string SETTING_VIEW_NAME = "ATS.Plugins.Template.Views.Options";
    private const string TOP_MENU_NAME = "Plugins";

    public void OnInitialized(IContainerProvider containerProvider)
    {
        // 注册菜单
        var navigationManager = containerProvider.Resolve<INavigationManager>(); // 获取导航配置管理接口

        // 添加一级菜单
        navigationManager.Add(
            new NavigationViewItem(
                name: TOP_MENU_NAME,  // 菜单名称，必须唯一，
                title: "插件系统",    // 菜单显示名称
                icon: "&#xe209;",     // 菜单图标，https://docs.telerik.com/devtools/wpf/styling-and-appearance/glyphs/common-styles-appearance-glyphs-radglyph
                sort: 500,            // 排序，数字越小越靠前
                permission: Permissions.Default.MenuPlugins  // 权限，没有权限则不显示
                ));

        // 添加二级菜单
        navigationManager.AddSubItem(
            parentName: TOP_MENU_NAME,      // 父菜单名称
            child: new NavigationViewItem(
                 name: "Plugins.Template",  // 菜单名称，必须唯一，
                 title: "模板插件",         // 菜单显示名称
                 icon: "&#xe209;",          // 菜单图标，https://docs.telerik.com/devtools/wpf/styling-and-appearance/glyphs/common-styles-appearance-glyphs-radglyph
                 sort: 1,                   // 排序，数字越小越靠前
                 permission: Permissions.Default.MenuPluginsTemplate,  // 权限，没有权限则不显示
                 view: ENTRY_VIEW_NAME      // 关联的视图
             ));

        // 配置导航
        var  regionManager = containerProvider.Resolve<IRegionManager>(); // 获取导航区域管理接口
        regionManager.RegisterViewWithRegion<Views.Main>(ENTRY_VIEW_NAME);

        // 注册权限
        var permissionCatalog = containerProvider.Resolve<IPermissionCatalog>();
        Permissions.Create(permissionCatalog);

        // 注册设置项
        var settingDefinitionManager = containerProvider.Resolve<ISettingDefinitionManager>();
        SettingConfiguration.CreateSettings(settingDefinitionManager);

        var settingViewCatalog = containerProvider.Resolve<ISettingViewCatalog>();
        settingViewCatalog.Add(
           new NavigationViewItem("Plugins", "插件模板", sort: 200)
               .AddSubItem(new NavigationViewItem("Example", "常规", view: SETTING_VIEW_NAME))
       );
    }

    /// <summary>
    ///  注册插件的依赖服务
    /// </summary>
    /// <param name="containerRegistry"></param>
    public void RegisterTypes(IContainerRegistry containerRegistry)
    {
        // 注册页面导航
        containerRegistry.RegisterForNavigation<Views.Main>(ENTRY_VIEW_NAME);

        containerRegistry.RegisterForNavigation<Views.Options>(SETTING_VIEW_NAME);
    }
}